package com.one.rope.mvp.web.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.one.rope.mvp.basecore.util.MyBeanUtils;
import com.one.rope.mvp.basecore.util.StringUtils;
import com.one.rope.mvp.web.business.dao.CompanyPartnerIncentiveMapper;
import com.one.rope.mvp.web.business.entity.CompanyPartnerIncentive;
import com.one.rope.mvp.web.business.entity.CompanyProjectPartner;
import com.one.rope.mvp.web.business.dao.CompanyProjectPartnerMapper;
import com.one.rope.mvp.web.business.service.ICompanyProjectPartnerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.one.rope.mvp.web.common.dto.admin.partner.CompanyPartnerDetailVO;
import com.one.rope.mvp.web.common.dto.admin.partner.AdminEditPartnerDTO;
import com.one.rope.mvp.web.common.dto.admin.partner.IncentiveItemDetailVO;
import com.one.rope.mvp.web.common.dto.admin.partner.IncentiveItemVO;
import com.one.rope.mvp.basecore.bean.AdminBusinessMessage;
import com.one.rope.mvp.basecore.bean.ResponseObject;
import com.one.rope.mvp.web.common.dto.user.business.PartnerListItemVO;
import com.one.rope.mvp.basecore.exception.BusinessDataException;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 项目合伙人岗位表 服务实现类
 * </p>
 *
 * @author wesley
 * @since 2020-02-20
 */
@Service
public class CompanyProjectPartnerServiceImpl extends
    ServiceImpl<CompanyProjectPartnerMapper, CompanyProjectPartner> implements
    ICompanyProjectPartnerService {

  @Autowired
  CompanyPartnerIncentiveMapper companyPartnerIncentiveMapper;

  @Override
  @Transactional(rollbackFor = Exception.class)
  public ResponseObject editPartner(AdminEditPartnerDTO requestDTO) {
    CompanyProjectPartner companyProjectPartner = new CompanyProjectPartner();
    MyBeanUtils.copyPropertiesIgnoreNull(requestDTO, companyProjectPartner);
    // 更新合伙人信息
    this.updateById(companyProjectPartner);

    // 更新合伙人的激励方案
    List<IncentiveItemVO> incentiveItemVOList = requestDTO.getIncentiveList();
    if (null != incentiveItemVOList && incentiveItemVOList.size() > 0) {
      incentiveItemVOList.stream().forEach(item -> {
        CompanyPartnerIncentive companyPartnerIncentive = new CompanyPartnerIncentive();
        MyBeanUtils.copyPropertiesIgnoreNull(item, companyPartnerIncentive);
        if (null == item.getId() || item.getId() == 0) {
          // 新增
          companyPartnerIncentive.setId(null);
          companyPartnerIncentive.setPartnerId(companyProjectPartner.getId());
          companyPartnerIncentiveMapper.insert(companyPartnerIncentive);
        } else {
          // 编辑
          companyPartnerIncentiveMapper.updateById(companyPartnerIncentive);
        }
      });
    }

    return ResponseObject.successObject();
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void deletePartner(Long id) {
    CompanyProjectPartner companyProjectPartner = this.getById(id);
    if (null == companyProjectPartner) {
      throw new BusinessDataException(AdminBusinessMessage.COMPANY_PARTNER_NOT_EXIST);
    }
    // 删除合伙人信息表
    this.removeById(id);
    // 删除合伙人激励方案表
    companyPartnerIncentiveMapper.delete(new QueryWrapper<CompanyPartnerIncentive>().eq("partner_id", id)
    );
  }

  @Override
  public CompanyPartnerDetailVO partnerDetail(Long id) {
    CompanyProjectPartner companyProjectPartner = this.getById(id);
    if (null == companyProjectPartner) {
      throw new BusinessDataException(AdminBusinessMessage.COMPANY_PARTNER_NOT_EXIST);
    }
    CompanyPartnerDetailVO resultVO = new CompanyPartnerDetailVO();
    MyBeanUtils.copyPropertiesIgnoreNull(companyProjectPartner, resultVO);
    // 查询设置的激励方案
    QueryWrapper<CompanyPartnerIncentive> incentiveQueryWrapper = new QueryWrapper<>();
    incentiveQueryWrapper.eq("partner_id", companyProjectPartner.getId());
    incentiveQueryWrapper.eq("is_run", 1);
    List<CompanyPartnerIncentive> partnerIncentiveList = companyPartnerIncentiveMapper
        .selectList(incentiveQueryWrapper);
    List<IncentiveItemDetailVO> incentiveList = new ArrayList<>();
    if (partnerIncentiveList.size() > 0) {
      partnerIncentiveList.stream().forEach(item -> {
        IncentiveItemDetailVO itemVO = new IncentiveItemDetailVO();
        MyBeanUtils.copyPropertiesIgnoreNull(item, itemVO);

        if (!StringUtils.isNullOrEmpty(item.getCompanyFactor())) {
          itemVO.setCompanyFactorList(CollectionUtils.asList(item.getCompanyFactor().split(",")));
        }

        if (!StringUtils.isNullOrEmpty(item.getPersonalFactor())) {
          itemVO.setPersonalFactorList(CollectionUtils.asList(item.getPersonalFactor().split(",")));
        }

        if (!StringUtils.isNullOrEmpty(item.getCompanyExertion())) {
          itemVO.setCompanyExertionList(CollectionUtils.asList(item.getCompanyExertion().split(",")));
        }

        if (!StringUtils.isNullOrEmpty(item.getPersonalExertion())) {
          itemVO.setPersonalExertionList(CollectionUtils.asList(item.getPersonalExertion().split(",")));
        }
        incentiveList.add(itemVO);
      });
    }
    resultVO.setIncentiveList(incentiveList);
    return resultVO;
  }

  @Override
  public List<PartnerListItemVO> selectPartnerList(Long companyId) {
    return baseMapper.selectPartnerList(companyId);
  }
}
